<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PayPlatController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function index()
    {
        $list = DB::table('pay_platform')
            ->orderBy('id', 'desc')
            ->get();
        return view('admin.payPlat.index', ['list' => $list]);
    }

    public function edit($id)
    {
        $detail = DB::table('pay_platform')->find($id);
        return view('admin.payPlat.edit', ['detail' => $detail]);
    }

    public function update(Request $request, $id)
    {
        //验证表单
        $this->validate($request, [
            'plat_name' => ['required','string'],
            'app_key' => ['required','string'],
            'game_id' => ['required','string'],
            'group_id' => ['required','string'],
            'type_id' => ['required','string'],
            'user_id' => ['required','string'],
            'pay_version' => ['required','string'],
            'is_rand' => ['required','int'],
        ]);
        if($id != 0){
            // 更新
            DB::table('pay_platform')
                ->where('id', $id)
                ->update([
                    'plat_name' => $request->plat_name,
                    'app_key' => $request->app_key,
                    'game_id' => $request->game_id,
                    'group_id' => $request->group_id,
                    'type_id' => $request->type_id,
                    'user_id' => $request->user_id,
                    'pay_version' => $request->pay_version,
                    'is_rand' => $request->is_rand,
                ]);
        }else{
            // 创建
            DB::table('pay_platform')
                ->insert([
                    'plat_name' => $request->plat_name,
                    'app_key' => $request->app_key,
                    'game_id' => $request->game_id,
                    'group_id' => $request->group_id,
                    'type_id' => $request->type_id,
                    'user_id' => $request->user_id,
                    'pay_version' => $request->pay_version,
                    'create_time' => date('Y-m-d H:i:s'),
                    'is_rand' => $request->is_rand,
                ]);
        }
        return successResp('');
    }

    public function destroy($id)
    {
        DB::table('pay_platform')
            ->whereIn('id', explode(',', $id))
            ->delete();
        return successResp('');
    }
}
